//
//  WFEmoticonToolBar.swift
//  WangFangWeiBo
//
//  Created by 王方 on 16/7/19.
//  Copyright © 2016年 wf. All rights reserved.
//

import UIKit

enum WFEmoticonToolBarButtonType: Int{
    //默认
    case Normal = 0
    // Emoji
    case Emoji = 1
    //浪小花
    case LXH = 2
}

class WFEmoticonToolBar: UIStackView {

    //定义按钮数组存储三个按钮
    private lazy var kimojiBtns:[UIButton] = {
        var btns:[UIButton] = [UIButton]()
        return btns
    }()
    
    //定义上次点击的按钮记录状态
    var lastSectedBtn:UIButton?
    //定义闭包回调
    var emojiBtnToolBarClosure:((type:WFEmoticonToolBarButtonType)->())?
    
    // MARK:  - 重写构造
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MARK:  - 对外提供一个借口用于改变toolbar的对应按钮选择
    func clickOnBtnIcon(type:WFEmoticonToolBarButtonType){
        for value in kimojiBtns {
            if type == WFEmoticonToolBarButtonType(rawValue: value.tag) {

//                self.emojiBtnClick(value)
                if lastSectedBtn == value {
                    return
                }
                
                lastSectedBtn?.selected = false
                value.selected = true
                lastSectedBtn = value
                
            }
        }
    }
    // MARK:  - 初始化控件
    private func setUpUI(){
        //设置starkview
        axis = .Horizontal
        distribution = .FillEqually
        
        
        
        //添加三个toolbarbtn
       let defaultBtn =  addChildBtn("默认", imageName: "compose_emotion_table_left", type: .Normal)
         addChildBtn("Emoji", imageName: "compose_emotion_table_mid", type: .Emoji)
        addChildBtn("浪小花", imageName: "compose_emotion_table_right", type: .LXH)
        
        //一进入页面就让默认按钮被点击
        self.emojiBtnClick(defaultBtn)
    }
    
    
    // MARK:  - 添加子按钮
    private func addChildBtn(title: String, imageName: String, type: WFEmoticonToolBarButtonType)->UIButton{
        let btn = UIButton()
        
        btn.tag = type.rawValue
        btn.setTitle(title, forState: .Normal)
        btn.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        btn.setTitleColor(UIColor.grayColor(), forState: .Selected)
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        btn.setBackgroundImage(UIImage(named: "\(imageName)_normal"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "\(imageName)_selected"), forState: .Selected)
        //取消高亮
        btn.adjustsImageWhenHighlighted = false
        btn.addTarget(self, action:"emojiBtnClick:", forControlEvents: .TouchUpInside)
        addArrangedSubview(btn)
        kimojiBtns.append(btn)
        return btn
    }
    
    // MARK:  - 点击最下方的toolbar按钮执行的方法
    @objc private func emojiBtnClick(btn:UIButton){

        if lastSectedBtn == btn {
            return
        }
        
        lastSectedBtn?.selected = false
        btn.selected = true
        lastSectedBtn = btn
        
       
            let type = WFEmoticonToolBarButtonType(rawValue: btn.tag)
            //执行闭包
            emojiBtnToolBarClosure?(type: type!)
        
        
        
    }
}
